package com.xiaov.android.games.my3d;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.Bitmap;

import com.xiaov.android.games.my3d.mesh.SimplePlane;
import com.xiaov.math.Vector3D;

public class ShotBar {
	float width = 64;
	float height = 400;
	float x = 400;
	float y = 0 * GameView.width / 854;
	float z = 100;
	
	boolean shouldDisplay = true;
	
	SimplePlane d;
	SelectPlane over;
	
	private boolean amotion = false;
	private float step = 0;
	
	public ShotBar(){
		d = new SimplePlane(width, height);
		over = new SelectPlane(width, width);
		d.x = x;
		d.y = y;
		d.z = z;
		over.x = x;
		over.y = y + height / 2 - width / 2;
		over.z = z + 0.5f;
	}
	
	public ShotBar(float x, float y, float width, float height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		d = new SimplePlane(width, height);
		over = new SelectPlane(width / 30, height / 30);
		d.x = x;
		d.y = y;
		d.z = z;
		over.x = x;
		over.y = y + height / 2 - width / 2;
		over.z = z + 0.5f;
	}
	
	public void bindBitmap(Bitmap bitmap1, Bitmap bitmap2, Bitmap bitmap3){
		d.loadBitmap(bitmap1);
		over.loadBitmap(bitmap2);
		over.loadBitmap(bitmap3);
	}
	
	public void draw(GL10 gl){
		if(shouldDisplay){
			d.draw(gl);
			over.draw(gl);
		}
	}
	
	public void startAmotion(){
		amotion = true;
		step = getOffset() / 5;
		step = step > 0 ? step : -step;
	}
	
	public void tick(){
		if(amotion){
			setOffset(step);
			float tmp = y + height / 2 - width / 2 - over.y;
			tmp = tmp > 0 ? tmp : -tmp;
			if(tmp < step){
				over.y = y + height / 2 - width / 2;
				amotion = false;
			}
		}
	}
	
	public int getOffset(){
		return  (int)(((y + height / 2 - width / 2) - over.y) / (y + height / 2 - width / 2) * 100);
	}
	
	public void setOffset(float oy){
		
		float yy = over.y + oy - (y + height / 2 - width / 2);
		if(-yy > height - width || -yy < 0){
			return;
		}
		
		over.y += oy;
	}
	
	public void display(){
		shouldDisplay = true;
	}
	
	public void hidden(){
		shouldDisplay = false;
	}
}
